// #include "BlockQueue.hpp"
// using namespace ts;

// void* consumer(void *args)
// {
//     BlockQueue<int>* bq = (BlockQueue<int>*)args;
//     while(true)
//     {
//         //消费者不断消费
//         sleep(2);
//         int data = 0;
//         bq->pop(&data);
//         std::cout << "消费者消费数据：" << data << std::endl;
//     }
// }

// void* producter(void* args)
// {
//     BlockQueue<int>* bq = (BlockQueue<int>*)args;
//     while(true)
//     {
//         //生产者不断生产
//         //sleep(2);
//         int data = rand()%20 + 1;
//         std::cout << "生产者生产数据: " << data << std::endl;
//         bq->push(data);
//     }
// }

// int main()
// {
//     srand((long long)time(nullptr));
//     BlockQueue<int> *bq = new BlockQueue<int>;
//     pthread_t c;
//     pthread_t p;
//     pthread_create(&c, nullptr, consumer, bq);
//     pthread_create(&p, nullptr, producter, bq);

//     pthread_join(c, nullptr);
//     pthread_join(p, nullptr);
//     return 0;
// }

#include "BlockQueue.hpp"
#include "Task.hpp"
using namespace ts;
using namespace ns_task;

void *consumer(void *args)
{
    BlockQueue<Task> *bq = (BlockQueue<Task> *)args;
    
    while (true)
    {
        Task t;
        bq->pop(&t);
        t();
        //sleep(1);
    }
}

void *producter(void *args)
{
    BlockQueue<Task> *bq = (BlockQueue<Task> *)args;
    std::string ops = "+-*/%";
    while (true)
    {
        int x = rand() % 20 + 1; //[1, 20]
        int y = rand() % 10 + 1; //[1, 10]
        char op = ops[rand() % 5];
        std::cout << "当前线程是" << pthread_self() << "生产的任务是" << x << " " << y << " " << op << std::endl;
        Task t(x, y, op);
        bq->push(t);
        sleep(1);
    }
}

int main()
{
    srand((long long)time(nullptr));
    BlockQueue<Task> *bq = new BlockQueue<Task>;
    //多消费者,执行任务
    pthread_t c1;
    pthread_t c2;
    pthread_t c3;
    pthread_t c4;
    pthread_t p;
    pthread_create(&c1, nullptr, consumer, bq);
    pthread_create(&c2, nullptr, consumer, bq);
    pthread_create(&c3, nullptr, consumer, bq);
    pthread_create(&c4, nullptr, consumer, bq);
    pthread_create(&p, nullptr, producter, bq);

    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);
    pthread_join(c4, nullptr);
    pthread_join(p, nullptr);
    delete bq;
    return 0;
}
